﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KeMei.Common;

public partial class Manager_aboutUs_Edit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserInfo"] == null)
        {
            Response.Redirect(ConfigHelper.SystemUrl + "Manager/login.aspx", true);

        }
        if (!IsPostBack)
        {
            this.Initlm(true);
            if (Request["ID"] != null)
            {
                this.Initlm(false);
                this.InitModel(int.Parse(Request["ID"].ToString()));
            }
        }
    }

    /// <summary>
    /// 绑定实体类到控件
    /// </summary>
    protected void InitModel(int ID)
    {
        KeMei.Model.AboutUs model = null;
        KeMei.BLL.AboutUs bll = new KeMei.BLL.AboutUs();

        try
        {
            model = bll.GetModel(ID);

            if (model != null)
            {
                txtcontent.Value = model.TContent;
                ddllmType.SelectedValue = model.lmID.ToString();
                ddllmType.Enabled = false;
            }
        }
        catch (Exception ex)
        {

            ExtAspNet.Alert.Show("初始化实体类失败：" + ex.Message);
        }
    }


    /// <summary>
    /// 初始化栏目
    /// </summary>
    protected void Initlm(bool isAdd)
    {
        try
        {
            List<KeMei.Model.Syslm> list = null;
            KeMei.BLL.Syslm bll = new KeMei.BLL.Syslm();

            if (isAdd)
            {
                list = bll.GetModelList("ParentID=4 and ID not in(select lmId from AboutUs)");
            }
            else
            {
                list = bll.GetModelList("ID in(select lmId from AboutUs)");
            }
            if (list == null)
            {
                ddllmType.Enabled = false;
            }
            ddllmType.DataSource = list;
            ddllmType.DataTextField = "TName";
            ddllmType.DataValueField = "ID";
            ddllmType.DataBind();
        }
        catch (Exception ex)
        {

            ExtAspNet.Alert.Show("绑定栏目失败：" + ex.Message);
        }
    }




    /// <summary>
    /// 保存
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSave_Click(object sender, EventArgs e)
    {
        KeMei.BLL.AboutUs bll = new KeMei.BLL.AboutUs();
        KeMei.Model.AboutUs model = new KeMei.Model.AboutUs();

        try
        {
            if (string.IsNullOrEmpty(txtcontent.Value.Trim()))
            {
                ExtAspNet.Alert.Show("请填写内容");
                return;
            }


            model.lmID = int.Parse(ddllmType.SelectedValue);
            model.TContent = txtcontent.Value;
            model.Recorddate = DateTime.Now;

            if (Request["ID"] != null)
            {
                model.ID = int.Parse(Request["ID"].ToString());

                bll.Update(model);
            }
            else
            {
                bll.Add(model);
            }
            ExtAspNet.Alert.Show("保存成功！", String.Empty, ExtAspNet.Alert.DefaultIcon, ExtAspNet.ActiveWindow.GetHidePostBackReference());
        }
        catch (Exception ex)
        {
            ExtAspNet.Alert.Show("保存失败：" + ex.Message);
        }
    }
}